perf: Optimize scalar path for chr function#20073
Merged
Jefffrey merged 2 commits intoapache:mainfrom Feb 1, 2026
Merged
Conversation
Contributor
Author
Contributor
Author
martin-g
approved these changes
Jan 30, 2026
Member
martin-g
left a comment
There was a problem hiding this comment.
LGTM!
It would be good to add some SLT tests though.
There are only array based unit tests and there are no SLT tests.
The Spark ones are empty - https://github.com/apache/datafusion/blob/f0de02fd664afcc4aad61fd8d13503533ed1e8d5/datafusion/sqllogictest/test_files/spark/string/chr.slt
Jefffrey
reviewed
Jan 30, 2026
| let return_type = args.return_field.data_type(); | ||
| let [arg] = take_function_args(self.name(), args.args)?; | ||
|
|
||
| match arg { |
Contributor
There was a problem hiding this comment.
match arg {
ColumnarValue::Scalar(ScalarValue::Int64(Some(code_point))) => {
if let Ok(u) = u32::try_from(code_point)
&& let Some(c) = core::char::from_u32(u)
{
Ok(ColumnarValue::Scalar(ScalarValue::Utf8(Some(
c.to_string(),
))))
} else {
exec_err!("invalid Unicode scalar value: {code_point}")
}
}
ColumnarValue::Scalar(ScalarValue::Int64(None)) => {
Ok(ColumnarValue::Scalar(ScalarValue::Utf8(None)))
}
ColumnarValue::Array(array) => {
Ok(ColumnarValue::Array(chr(&[array])?))
}
_ => internal_err!("..."),
}- Easier to fold into match like this since we accept only one datatype; also unnecessary to double check the array datatype
- Also worth looking into refactoring
chr()function; no reason to pass in a slice when we can just pass the array
Contributor
Author
Thank you for the feedback. |
Jefffrey
approved these changes
Jan 31, 2026
Contributor
|
Thanks @kumarUjjawal & @martin-g |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.


Which issue does this PR close?
Rationale for this change
chrcurrently routes scalar inputs throughmake_scalar_function(chr, vec![]), which performs a scalar → size-1 array → scalar roundtrip. This adds unnecessary overhead for constant folding / scalar evaluation.This PR adds a match-based scalar fast path and removes reliance on
make_scalar_functionforchr, while keeping the array behavior unchanged.What changes are included in this PR?
ChrFunc::invoke_with_argsto:ColumnarValue::Scalar(Int64)directly (scalar fast path)ColumnarValue::Array(Int64Array)using the existing conversion logicbenches/chr.rs(chr/scalar) outside any size loopchr/scalarAre these changes tested?
Yes
Are there any user-facing changes?
No